import { defineStore } from 'pinia';

export const useTimestampStore = defineStore('timestamp', {
    state: () => ({
        currentTimestamp: Date.now(), // 当前时间戳
        initializedAt: Date.now()     // store初始化时间
    }),

    actions: {
        /**
         * 创建新的时间戳（原updateTimestamp方法重命名）
         */
        createTimestamp() {
            this.currentTimestamp = Date.now();
        },

        /**
         * 获取当前时间戳
         */
        getTimestamp() {
            return this.currentTimestamp;
        },

        /**
         * 获取格式化的时间字符串
         * @param format 格式模板（默认"YYYY-MM-DD HH:mm:ss"）
         */
        getFormattedTime(format = "YYYY-MM-DD HH:mm:ss") {
            const date = new Date(this.currentTimestamp);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');

            return format
                .replace(/YYYY/g, year.toString())
                .replace(/MM/g, month)
                .replace(/DD/g, day)
                .replace(/HH/g, hours)
                .replace(/mm/g, minutes)
                .replace(/ss/g, seconds);
        }
    }
});
